<?php
include '../../../wp-blog-header.php';
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("delivery-chef.xls");
$api = new PodAPI();
echo "<pre>";
$excel_data = $data->sheets['0']['cells'];
$excel_columns = array_shift($excel_data);
//print_r($excel_data);
//exit();

foreach($excel_data as $key=>$value){
  $columns_deal['name'] = pods_sanitize(ucwords(strtolower($value[4].': '.$value[1])));
  $columns_deal['camp_link'] = sanitize_title($columns_deal['name']);
  
  $deal = new Pod('deal');
  $deal->page=1;
  $deal->search=false;
  $where = "t.camp_link = '".$columns_deal['camp_link']."' AND t.sale = 1";
  $deal->findRecords('id DESC', 1,$where);
  $dealsFound = $deal->getTotalRows();
  
  if($dealsFound == 0)
  {
    $columns_deal['deal_address'] = pods_sanitize($value[10]);
    $columns_deal['short'] = pods_sanitize($value[10]);
    $columns_deal['terms'] = pods_sanitize($value[14]);
    $columns_deal['lat'] = $value[11];
    $columns_deal['lng'] = $value[12];
    
    $pos = strpos($value[4], '-');
    if ($pos === false){
        $brandurl = sanitize_title($value[4]);
    } else{
        $brandurl = explode('-',$value[4]);
        $brandurl = sanitize_title($brandurl[0]);
    }

    $brand_pod = new Pod('brand');
    $brand_pod->page=1;
    $brand_pod->search=false;
    $where = "t.brandurl = '".$brandurl."' AND t.local = 1";
    $brand_pod->findRecords('id DESC', 1,$where);
    while($brand_pod->fetchRecord())
    {
      $brand_id = $brand_pod->get_field('id');  
    }
    $columns_deal['brand'] = $brand_id;
    
    $relatedbrand = new Pod('brand',$brand_id);
    $image_url = $relatedbrand->get_field('logo.guid');
    $columns_deal['ext_image_url'] = $image_url;
    
    $columns_deal['deal_provider'] = '258';//delivery chef
    
    $columns_deal['camp_start'] = date('Y-m-d 12:00:00',strtotime($value[8]));
    if(empty($columns_deal['camp_start'])){
      $columns_deal['camp_start'] = date('Y-m-d 12:00:00');
    }
    
    $columns_deal['camp_end'] = date('Y-m-d 12:00:00',strtotime($value[9]));
    if(empty($columns_deal['camp_end']) || $columns_deal['camp_end'] == '1970-01-01 12:00:00'){
      $columns_deal['camp_end'] = date('Y-m-d H:i:s',strtotime('+3 month',strtotime(date("Y-m-d 12:00:00"))));
    }
     
    $deal_link = $value[2];
    $shortened_url = make_bitly_url($deal_link);
    if(empty($shortened_url))
    {
      $shortened_url = make_goog_url($deal_link);
    }
    
    if(empty($shortened_url))
    {
      $shortened_url = $deal_link;
    }
    $columns_deal['extlink'] = $shortened_url;
    
    $columns_deal['city'] = '1';
    $columns_deal['sale'] = '1';
    $columns_deal['camp_cat'] = '24';//restaurants & clubs
    
    $params = array('datatype' => 'deal','columns' => $columns_deal);
    
    echo '<pre>';
    print_r($params);
    echo '</pre>';
    //exit();
   
    
    //$api->save_pod_item($params);
  }
}
?>